package com.ruoyi.galaxy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.galaxy.api.*;
import com.ruoyi.galaxy.api.order.GalaxyApiOrder;
import com.ruoyi.galaxy.api.project.GalaxyApiProject;
import com.ruoyi.galaxy.api.project.GalaxyApiProjectDetail;
import com.ruoyi.galaxy.api.project.GalaxyApiProjectEvent;
import com.ruoyi.galaxy.api.shoppingCart.GalaxyApiShoppingCartCheckGoodSale;
import com.ruoyi.galaxy.api.trade.GalaxyApiTradeAddCart;
import com.ruoyi.galaxy.api.trade.GalaxyApiTradeConfirmGroupedCart;
import com.ruoyi.galaxy.api.trade.GalaxyApiTradeSubmitGroupedCart;
import com.ruoyi.galaxy.service.IGalaxyApiRequestService;
import com.ruoyi.galaxy.service.IGalaxyProjectService;
import com.ruoyi.galaxy.vo.*;
import com.ruoyi.galaxy.vo.order.GalaxyApiOrderListResponse;
import com.ruoyi.galaxy.vo.project.GalaxyApiProjectDetailResponse;
import com.ruoyi.galaxy.vo.project.GalaxyApiProjectEventResponse;
import com.ruoyi.galaxy.vo.project.GalaxyApiProjectListResponse;
import com.ruoyi.galaxy.vo.shoppingcart.GalaxyApiShoppingCartCheckGoodSaleRequest;
import com.ruoyi.galaxy.vo.shoppingcart.GalaxyApiShoppingCartCheckGoodSaleResponse;
import com.ruoyi.galaxy.vo.trade.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yeoman
 * @date 2025/4/10 19:38
 * @description GalaxyApiServiceImpl
 */
@Component
public class GalaxyApiRequestServiceImpl implements IGalaxyApiRequestService {

    public static final Logger log = LoggerFactory.getLogger(GalaxyApiRequestServiceImpl.class);

    @Resource
    private IGalaxyProjectService galaxyProjectService;

    @Override
    public GalaxyApiProjectListResponse getProjectList() {
        GalaxyApiProject api = new GalaxyApiProject();
        return (GalaxyApiProjectListResponse)api.execute(null, null, GalaxyApiEnum.GET_PROJECT_LIST);
    }

    @Override
    public GalaxyApiProjectDetailResponse selectProjectDetail(String projectToken) {
        GalaxyApiProjectDetail api = new GalaxyApiProjectDetail();
        return (GalaxyApiProjectDetailResponse)api.execute(projectToken, null, GalaxyApiEnum.GET_PROJECT);
    }

    @Override
    public GalaxyApiProjectEventResponse selectProjectEvent(String eventToken, Long userId) {
        GalaxyApiProjectEvent api = new GalaxyApiProjectEvent();
        return (GalaxyApiProjectEventResponse)api.execute(eventToken, userId, GalaxyApiEnum.GET_PROJECT_EVENT);
    }

    @Override
    public GalaxyApiOrderListResponse getOrderList(Long userId) {
        GalaxyApiOrder api = new GalaxyApiOrder();
        return (GalaxyApiOrderListResponse)api.execute(null, userId, GalaxyApiEnum.GET_ORDER_LIST);
    }

    @Override
    public Boolean palaceOrder(GalaxyApiPalaceOrderRequest request) {
        log.info("下单请求参数：{}", JSONObject.toJSONString(request));
        String eventId = request.getEventToken();
        Long userId = request.getUserId();

        // 0.获取场次票档信息
        GalaxyApiProjectEventResponse eventRes = galaxyProjectService.selectProjectEvent(eventId, userId);
        log.info("0.场次票档信息获取成功：{}", JSONObject.toJSONString(eventRes));
        GalaxyApiProjectEventResponse.Price priceRes = eventRes.getPriceVoList().stream().filter(priceVo -> priceVo.getPriceId().equals(request.getPriceId())).findFirst()
                .orElseThrow(() -> new ServiceException("场次票档信息不存在"));

        // 1.加购物车
        GalaxyApiTradeAddCartRequest addCartRequest = new GalaxyApiTradeAddCartRequest();
        addCartRequest.setEventId(eventId);
        addCartRequest.setToken(eventRes.getToken());
        List<GalaxyApiTradeAddCartRequest.CartGoods> cartGoodsList = new ArrayList<>();
        addCartRequest.setCartGoods(cartGoodsList);
        GalaxyApiTradeAddCartRequest.CartGoods cartGoods = new GalaxyApiTradeAddCartRequest.CartGoods();
        cartGoodsList.add(cartGoods);
        cartGoods.setGoodCount(1);
        cartGoods.setPrice(priceRes.getPrice());
        cartGoods.setPriceId(request.getPriceId());
        GalaxyApiTradeAddCart addApi = new GalaxyApiTradeAddCart();
        GalaxyApiTradeAddCartResponse addRes = (GalaxyApiTradeAddCartResponse) addApi.execute(addCartRequest, userId, GalaxyApiEnum.TRADE_ADD_CART);
        log.info("1.加购物车成功，addRes：{}", JSONObject.toJSONString(addRes));

        // 2.检查商品是否可售
        //GalaxyApiShoppingCartCheckGoodSale checkApi = new GalaxyApiShoppingCartCheckGoodSale();
        //GalaxyApiShoppingCartCheckGoodSaleRequest checkReq = new GalaxyApiShoppingCartCheckGoodSaleRequest();
        //checkReq.setToken(addRes.getToken());
        //checkReq.setEventId(eventId);
        //GalaxyApiShoppingCartCheckGoodSaleResponse checkRes = (GalaxyApiShoppingCartCheckGoodSaleResponse) checkApi.execute(checkReq, userId, GalaxyApiEnum.SHOPPING_CART_CHECK_GOOD_SALE);
        //log.info("2.检查商品是否可售成功，checkRes：{}", JSONObject.toJSONString(checkRes));

        // 3.确认订单
        //GalaxyApiTradeConfirmGroupedCart confirmApi = new GalaxyApiTradeConfirmGroupedCart();
        //GalaxyApiTradeConfirmGroupedCartRequest confirmReq = new GalaxyApiTradeConfirmGroupedCartRequest();
        //confirmReq.setToken(checkRes.getToken());
        //GalaxyApiTradeConfirmGroupedCartResponse confirmRes = (GalaxyApiTradeConfirmGroupedCartResponse) confirmApi.execute(confirmReq, userId, GalaxyApiEnum.TRADE_CONFIRM_CART);
        //log.info("3.确认订单成功，confirmRes：{}", JSONObject.toJSONString(confirmRes));

        // 4.提交
        GalaxyApiTradeSubmitGroupedCart submitApi = new GalaxyApiTradeSubmitGroupedCart();
        GalaxyApiTradeSubmitGroupedCartRequest submitReq = new GalaxyApiTradeSubmitGroupedCartRequest();
        //submitReq.setToken(confirmRes.getToken());
        submitReq.setReceivableMoney(priceRes.getPrice());
        submitReq.setProjectId(request.getProjectToken());
        submitReq.setOrderReceiveParam(new GalaxyApiTradeSubmitGroupedCartRequest.OrderReceiveParam());
        GalaxyApiTradeSubmitGroupedCartResponse submitRes = (GalaxyApiTradeSubmitGroupedCartResponse) submitApi.execute(submitReq, userId, GalaxyApiEnum.TRADE_SUBMIT_CART);
        log.info("4.提交订单成功，submitRes：{}", JSONObject.toJSONString(submitRes));

        return true;
    }

}
